package org.association.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.association.bo.OrganizationEntityExpandBO;
import org.association.entity.OrgUserStats;
import org.association.entity.OrganizationEntity;
import org.association.entity.UserEntity;
import org.association.service.OrganizationService;
import org.association.service.UserService;
import org.association.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 *
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2024-07-18 17:00:07
 */

@Api(tags = "协会接口")
@RestController
@RequestMapping("/organization")
@CrossOrigin
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @ApiOperation(value = "获取协会列表")
    @GetMapping("/list")
    public R list(){
        List<OrganizationEntity> organizationEntities = organizationService.list();
        List<OrgUserStats> orgUserStats = organizationService.getOrgUserStats();
        HashMap<Integer, Integer> map = new HashMap<>();
        for (OrgUserStats orgUserStat : orgUserStats) {
            map.put(orgUserStat.getOrgId(), orgUserStat.getCount());
        }

        List<OrganizationEntityExpandBO> res = new ArrayList<>();
        for (OrganizationEntity organizationEntity : organizationEntities) {
            OrganizationEntityExpandBO expandBO = new OrganizationEntityExpandBO();
            BeanUtils.copyProperties(organizationEntity, expandBO);
            expandBO.setUserCount(map.getOrDefault(expandBO.getId(), 0));
            res.add(expandBO);
        }
        return R.ok().put("list", res);
    }

    @ApiOperation(value = "会员加入协会")
    @ApiImplicitParams( value = {
            @ApiImplicitParam(name = "organizationID", value = "协会ID", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "privateKey", value = "私钥", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/add/{organizationID}/{privateKey}")
    public R addOrganization(@PathVariable Integer organizationID, @PathVariable String privateKey){

        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("private_key", privateKey);
        UserEntity entity = userService.getOne(queryWrapper);
        if(entity == null) {
            return R.error().message("用户不存在");
        }

        boolean addedMember = organizationService.userOrgExist(privateKey);
        if(addedMember) {
            return R.error().message("已加入协会");
        }
        boolean addSuccess = organizationService.addMember(organizationID, privateKey);
        if(!addSuccess) {
            return R.error().message("加入失败");
        }
        return R.ok();
    }

    @ApiOperation(value = "会员退出协会")
    @ApiImplicitParams( value = {
            @ApiImplicitParam(name = "organizationID", value = "协会ID", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "privateKey", value = "私钥", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/quit/{organizationID}/{privateKey}")
    public R quitOrganization(@PathVariable Integer organizationID, @PathVariable String privateKey){
        boolean addedMember = organizationService.userOrgExist(privateKey);
        if(!addedMember) {
            return R.error().message("未加入协会");
        }
        int quitSuccess = organizationService.quitMember(organizationID, privateKey);
        if(quitSuccess <= 0) {
            return R.error().message("退出失败");
        }
        return R.ok();
    }

    @ApiOperation(value = "一个协会下的所有信息")
    @ApiImplicitParams( value = {
            @ApiImplicitParam(name = "organizationID", value = "协会ID", dataTypeClass = Integer.class, required = true)
    })
    @GetMapping("/getOne/{organizationID}")
    public R addOrganization(@PathVariable Integer organizationID){
        if(organizationID == null) {
            return R.error().message("ID不能为空");
        }
        QueryWrapper<OrganizationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", organizationID);

        OrganizationEntity organizationEntity = organizationService.getOne(queryWrapper);
        if(organizationEntity == null) {
            return R.error().message("未知错误");
        }

        List<String> users = organizationService.getUsersByOrganizationID(organizationID);

        OrganizationEntityExpandBO expandBO = new OrganizationEntityExpandBO();
        BeanUtils.copyProperties(organizationEntity, expandBO);
        expandBO.setUsers(users);

        return R.ok().put("data", expandBO);
    }

}
